package com.bawei.module_mall

import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mall.adapter.OrderListAdapter
import com.bawei.module_mall.databinding.ActivityOrderListBinding
import com.bawei.module_mall.manager.OrderManager
import com.bawei.module_mall.viewmodel.OrderListViewModel

@Route(path = "/module_mall/OrderListActivity")
class OrderListActivity : BaseActivity() {

    private val binding by lazy {
        ActivityOrderListBinding.inflate(layoutInflater)
    }

    private val viewModel: OrderListViewModel by viewModels()
    private lateinit var orderAdapter: OrderListAdapter

    private val tabViews by lazy {
        listOf(
            binding.tvTabAll,
            binding.tvTabPendingPayment,
            binding.tvTabPendingShipment,
            binding.tvTabPendingReceipt,
            binding.tvTabPendingReview
        )
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViews()
        setupObservers()
        setupClickListeners()
        // 移除addTestData调用，页面初始为空
    }

    override fun initView() {
        super.initView()
        binding.tvShouquan.setOnClickListener {
            ARouter.getInstance().build("/module_mall/ShouActivity").navigation()
        }
    }

    private fun initViews() {
        // 设置RecyclerView
        orderAdapter = OrderListAdapter(
            orders = emptyList(),
            onAction1Click = { order ->
                viewModel.onAction1Click(order)
            },
            onAction2Click = { order ->
                viewModel.onAction2Click(order)
            }
        )

        binding.rvOrders.apply {
            layoutManager = LinearLayoutManager(this@OrderListActivity)
            adapter = orderAdapter
        }

        // 设置初始选中状态
        updateTabSelection(0)
    }

    private fun setupObservers() {
        viewModel.orders.observe(this) { orders ->
            orderAdapter.updateOrders(orders)
        }

        viewModel.selectedTab.observe(this) { position ->
            updateTabSelection(position)
        }

        viewModel.message.observe(this) { message ->
            if (message.isNotEmpty()) {
                showToast(message)
            }
        }
    }

    private fun setupClickListeners() {
        // 返回按钮
        binding.icBack.setOnClickListener {
            finish()
        }

        // 标签页点击事件
        tabViews.forEachIndexed { index, textView ->
            textView.setOnClickListener {
                viewModel.selectTab(index)
            }
        }
    }

    private fun updateTabSelection(selectedPosition: Int) {
        tabViews.forEachIndexed { index, textView ->
            if (index == selectedPosition) {
                textView.isSelected = true
                textView.setTextColor(ContextCompat.getColor(this, R.color.black))
                textView.textSize = 14f
            } else {
                textView.isSelected = false
                textView.setTextColor(ContextCompat.getColor(this, R.color.gray_dark))
                textView.textSize = 14f
            }
        }
    }

    override fun onResume() {
        super.onResume()
        // 每次页面恢复时刷新订单列表
        viewModel.refreshOrders()
    }

    // 显示消息提示
    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }

    // 删除addTestData方法
}